package com.topone.service.impl;

import com.topone.Enum.NotificationEnum;
import com.topone.Utils.ServiceUtils;
import com.topone.dataObject.PayNotification;
import com.topone.dataObject.WxServiceProviderNotification;
import com.topone.repository.NotificationRepository;
import com.topone.repository.ProviderNotificationRepository;
import com.topone.service.NotificationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class NotificationServiceImpl implements NotificationService {

    @Autowired
    private NotificationRepository repository;
    @Autowired
    private ProviderNotificationRepository providerNotificationRepository;


    @Override
    public void save(PayNotification data) {
        repository.save(data);
    }

    @Override
    public PayNotification savePayResult(PayNotification payNotification) {
        PayNotification data = null;
        log.info("-------------------payNotification--------------{}", ServiceUtils.toJson(payNotification));
        if (Objects.nonNull(payNotification)
                && StringUtils.isNotEmpty(payNotification.getOutTradeNo())) {
            try {
                data = repository
                        .findByOutTradeNo(payNotification.getOutTradeNo());
            } catch (Exception e) {
                log.error("根据交易单号查询失败-->", e.fillInStackTrace());
            }
            if (Objects.nonNull(data)) {
                payNotification.setPrepayId(data.getPrepayId());
                payNotification.setOrderId(data.getOrderId());
                payNotification.setId(data.getId());
                payNotification.setOutTradeNo(data.getOutTradeNo());
                payNotification.setType(data.getType());
                payNotification.setBussinessId(data.getBussinessId());
                payNotification.setBussinessName(data.getBussinessName());
                payNotification.setStatus(NotificationEnum.NO.getCode());
                if (StringUtils.isNotEmpty(data.getFlag())) {
                    payNotification.setFlag(data.getFlag());
                }
                if (StringUtils.isNotEmpty(data.getCouponCode())) {
                    payNotification.setCouponCode(data.getCouponCode());
                }
                repository.save(payNotification);
                return data;
            } else {
                log.error("[保存支付结果]无法查询到信息");
            }
        } else {
            log.error("[保存支付结果]无支付返回结果");
        }
        return null;
    }

    @Override
    public List<PayNotification> findListByBussinessId(String bussinessId, Integer status, String type) {
        return repository.findByBussinessIdAndStatusAndFlag(bussinessId,
                status, type);
    }


//服务商支付支付结果
    public void  saveProviderResult(WxServiceProviderNotification data){
        if (Objects.nonNull(data) && data.getId() != null) {
            WxServiceProviderNotification savedData = providerNotificationRepository.getOne(data.getId());
            BeanUtils.copyProperties(data, savedData);
            providerNotificationRepository.save(savedData);
        } else {
            providerNotificationRepository.save(data);
        }
    }
}
